package main.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import main.dao.UserDao;
import main.pojo.User;
import main.util.DBUtil;
import main.util.Logger;

public class UserDaoImpl implements UserDao{

	@Override
	public int addUser(User u) {
		Connection con = null;
		PreparedStatement pst = null;
		String sql = "insert into users(uname, sal, birth,sex,head_pic) values(?, ?, ?, ?, ?)";
		int m = 0;
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
//            pst.setString(1,new String(u.getUname().getBytes("UTF-8"),"UTF-16"));
			 pst.setString(1,u.getUname());
            pst.setDouble(2, u.getSal());
            pst.setDate(3, new java.sql.Date(u.getBirth().getTime()));
            pst.setBoolean(4, u.isSex());
            pst.setBytes(5, u.getHeadPic());
			m = pst.executeUpdate();  //返回执行的语句数目
		} catch (Exception e) {
			Logger.log("UserDao数据插入异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	@Override
	public int deleteUser(String name) {
		Connection con = null;
  		PreparedStatement pst = null;
  		String sql = "delete from users where uname = ?";
  		int m = 0;
  		try {
  			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, name);
			m = pst.executeUpdate();
		} catch (Exception e) {
			Logger.log("UserDao数据删除异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
  		return m;
	}

	@Override
	public int updateUser(User u) {
//		Connection con = null;
//		PreparedStatement pst = null;
//		String sql = "update users set uname=?, oonline = ? where uname = ?";
//		int m = 0;
//		try {
//			con = DBUtil.createConnection();
//			pst = con.prepareStatement(sql);
//			pst.setString(1, u.getPassWord());
//			pst.setBoolean(2, u.isOnline());
//			pst.setString(3, u.getUserName());
//			
//			m = pst.executeUpdate();   
//		} catch (Exception e) {
//			Logger.log("UserDao数据更新异常");
//			e.printStackTrace();
//		} finally {
//			DBUtil.close(con, pst, null);
//		}
//		return m;
		return 1;
	}

	@Override
	public User searchUser(String name) {
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  		String sql = "select * from users where uname = ?";
  		User user = null;
  		try {
  			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, name);
			rs = pst.executeQuery();
			
			if(rs.next()){
				user= new User(rs.getString("uname"), rs.getDouble("sal"),new Date(rs.getDate("birth").getTime()), rs.getBoolean("sex"),rs.getBytes("head_pic"));
			}
		} catch (Exception e) {
			Logger.log("UserDao数据查询异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
			return user;
		}
  		
  		//return user;
	}

	@Override
	public List<User> findAll() {
		List<User> list = new ArrayList<User>();
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  		String sql = "select * from users";
  		User user = null;
  		try {
  			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			
			while(rs.next()){
				user= new User(rs.getString("uname"), rs.getDouble("sal"),new Date(rs.getDate("birth").getTime()), rs.getBoolean("sex"),rs.getBytes("head_pic"));
				list.add(user);
			}
		} catch (Exception e) {
			Logger.log("UserDao数据查询异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		} 
  		
  		return list;
	}

}
